package servlet;

import db.BookDao;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import beans.Book;
import service.BookService;

import java.io.IOException;
import java.util.List;
@WebServlet("/Book")
public class BookSevrlet extends  HttpServlet {
    /**
     * Servlet implementation class Main
     */
    BookDao bookDao =new BookDao();
        private static final long serialVersionUID = 1L;

        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String action=request.getParameter("action");
//            if("login".equals(action))
//            {
//                login(request, response);
//            }
            System.out.println(action);
            if("addBook".equals(action))
            {
                addBook(request, response);
            }
            if("toDel".equals(action))
            {
                toDel(request, response);
            }
            if("delBook".equals(action))
            {
                delBook(request, response);
            }
            if("toEdit".equals(action))
            {
                toEdit(request, response);
            }
            if("editBook".equals(action))
            {
                editBook(request, response);
            }
            if("b1".equals(action))
            {
                getRecord(request, response);
                request.getRequestDispatcher("find.jsp").forward(request,response);
            }
            if("确认".equals(action)){
                List<Book> allRecords = bookDao.getAllRecords();
                request.setAttribute("allBook",allRecords);
                request.getRequestDispatcher("main.jsp").forward(request,response);
            }
        }

        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            doGet(request, response);
        }


    public List<Book> getRecord(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        String name = request.getParameter("name");
        BookService bookService = new BookService();
        List<Book>list = bookService.getRecord(name);
        request.setAttribute("books",list);
        return list;
    }



        public void addBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
        {
            BookService bookService=new BookService();
            //??????????????????????
            String id=request.getParameter("id");
            int idInt=Integer.parseInt(id);
            String name=request.getParameter("name");
            String press=request.getParameter("press");
            String price=request.getParameter("price");
            int typeInt=Integer.parseInt(price);
            String author=request.getParameter("author");
            //?????????????????????users??
            Book user=new Book();
            user.setBook_id(idInt);
            user.setBook_name(name);
            user.setBook_press(press);
            user.setBook_price(typeInt);
            user.setBook_author(author);
            if(bookService.addBook(user))
            {
                List <Book> list=bookService.browse();
                request.setAttribute("allBook", list);
                String path="/main.jsp";
                response.setContentType("text/html;charset=UTF-8");//???????????????????????????????out???
                RequestDispatcher requestDistpatcher=request.getRequestDispatcher(path);
                requestDistpatcher.forward(request, response);
            }
            else
            {
                String path="fail.jsp";
                response.setContentType("text/html;charset=UTF-8");//???????????????????????????????out???
                RequestDispatcher requestDistpatcher=request.getRequestDispatcher(path);
                requestDistpatcher.forward(request, response);
            }
        }

        public void delBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
        {
            BookService userService=new BookService();
            //??????????????????????
            String id=request.getParameter("id");
            int idInt=Integer.parseInt(id);
            String name=request.getParameter("name");
            String press=request.getParameter("press");
            String price=request.getParameter("price");
            int typeInt=Integer.parseInt(price);
            String author=request.getParameter("author");
            //?????????????????????users??
            Book user=new Book();
            user.setBook_id(idInt);
            user.setBook_name(name);
            user.setBook_press(press);
            user.setBook_author(author);
            user.setBook_price(typeInt);
            int delResult=userService.delUser(user);
            if(delResult==1)
            {
                List <Book> list=userService.browse();
                request.setAttribute("allBook", list);
                String path="/main.jsp";
                response.setContentType("text/html;charset=UTF-8");//???????????????????????????????out???
                RequestDispatcher requestDistpatcher=request.getRequestDispatcher(path);
                requestDistpatcher.forward(request, response);
            }
            else if(delResult==2)
            {
                String path="fail.jsp";
                response.setContentType("text/html;charset=UTF-8");//???????????????????????????????out???
                RequestDispatcher requestDistpatcher=request.getRequestDispatcher(path);
                requestDistpatcher.forward(request, response);
            }
//            else
//            {
//                String path="cantdel.jsp";
//                response.setContentType("text/html;charset=UTF-8");//???????????????????????????????out???
//                RequestDispatcher requestDistpatcher=request.getRequestDispatcher(path);
//                requestDistpatcher.forward(request, response);
//            }
        }

        public void editBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
        {
            BookService userService=new BookService();
            //??????????????????????
            String id=request.getParameter("id");
            int idInt=Integer.parseInt(id);
            String name=request.getParameter("name");
            String press=request.getParameter("press");
            String price=request.getParameter("price");
            int typeInt=Integer.parseInt(price);
            String author=request.getParameter("author");
            //?????????????????????users??
            Book user=new Book();
            user.setBook_id(idInt);
            user.setBook_name(name);
            user.setBook_press(press);
            user.setBook_author(author);
            user.setBook_price(typeInt);
            int editResult=userService.editUser(user);
            if(editResult==1)
            {
                List <Book> list=userService.browse();
                request.setAttribute("allBook", list);
                String path="/main.jsp";
                response.setContentType("text/html;charset=UTF-8");//???????????????????????????????out???
                RequestDispatcher requestDistpatcher=request.getRequestDispatcher(path);
                requestDistpatcher.forward(request, response);
            }
        }


        public void toDel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
        {
            String id=request.getParameter("id");//???????????????????????????????
            Book user=null;
            BookService userSer=new BookService();
            user=userSer.getRecordOne(Integer.parseInt(id));
            request.setAttribute("user", user);
            String path="/delete.jsp";
            response.setContentType("text/html;charset=UTF-8");//???????????????????????????????out???
            RequestDispatcher requestDistpatcher=request.getRequestDispatcher(path);
            requestDistpatcher.forward(request, response);
        }

        public void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
        {
            String id=request.getParameter("id");//???????????????????????????????
            Book user=null;
            BookService userSer=new BookService();
            user=userSer.getRecordOne(Integer.parseInt(id));
            request.setAttribute("user", user);
            String path="/edit.jsp";
            response.setContentType("text/html;charset=UTF-8");//???????????????????????????????out???
            RequestDispatcher requestDistpatcher=request.getRequestDispatcher(path);
            requestDistpatcher.forward(request, response);
        }

    }

